package com.md.rps.service;

import com.md.rps.mybatis.interfaces.I_HOSPITAL;
import com.md.rps.mybatis.model.HOSPITAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalOperator {

    @Autowired
    private I_HOSPITAL i_hospital;


    public List<HOSPITAL> getHospitalList(int start, int end){
        return i_hospital.getHospitalList(start,end);
    }

    public int addHospital(HOSPITAL hospital){
        int result = 0;
        // 首先判断医院名称是否被占用，在判断医院缩写是否被占用
        if (i_hospital.getHospitalByName(hospital.getNAME()) > 0) {
            // 2代表名称被占用
            result = 2;
        } else if (i_hospital.getHospitalByAbbr(hospital.getABBR()) > 0){
            // 3代表名称被占用
            result = 3;
        } else {
            // 执行成功返回1，失败0
            result = i_hospital.addHospital(hospital);
        }
        return result;
    }

    public int updateHospital(HOSPITAL hospital){
        int result = 0;
        // 首先判断医院是否存在
        if (i_hospital.getHospitalByID(hospital.getID()) == 0) {
            // 2代表医院不存在，无法修改
            result = 2;
        } else {
            result = i_hospital.updateHospital(hospital);
        }
        return result;
    }

    public int deleteHospital(int id){
        int result = 0;
        // 首先判断医院是否存在
        if (i_hospital.getHospitalByID(id) == 0) {
            // 2代表医院不存在，无法修改
            result = 2;
        } else {
            result = i_hospital.deleteHospital(id);
        }
        return result;
    }
}
